﻿using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Web.MvcPowered;
using Fuse8.DomainFramework.BusinessService;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsUserGroupController : CmsComplexEntityController<CmsUserGroup>
    {
        #region Fields

        private CmsUserGroupService _userGroupService = null;

        #endregion Fields

        #region Properties

        protected override IEntityBusinessService<CmsUserGroup, int> EntityService
        {
            get
            {
                return UserGroupService;
            }
        }

        protected virtual CmsUserGroupService UserGroupService
        {
            get
            {
                if (_userGroupService == null)
                {
                    _userGroupService = new CmsUserGroupService();
                }

                return _userGroupService;
            }
        }

        #endregion Properties

        #region View methods

        public virtual ActionResult Index(int? pageSize)
        {
            int currentPageSize = pageSize ?? DefaultPageSize;

            var data = UserGroupService.FindAll();

            ViewBag.PageSize = currentPageSize;
            ViewBag.Total = data.Count();

            return View(data);
        }

        [GridAction]
        public virtual ActionResult _Index(int? pageSize)
        {
            var data = UserGroupService.FindAll();

            return View(
                   new GridModel<CmsUserGroup>
                   {
                       Data = data
                   });
        }

        public virtual ActionResult Add()
        {
            return View(new CmsUserGroup());
        }

        [HttpPost]
        public virtual ActionResult Add(CmsUserGroup model)
        {
            ActionResult result = null;

            if (model != null)
            {
                if (string.IsNullOrEmpty(model.GroupName))
                {
                    ModelState.AddModelError("GroupName", GetText("Please enter group name", "cms_Users"));
                }

                if (ModelState.IsValid)
                {
                    using (var transaction = new BusinessServiceTransaction<CmsUserGroup>())
                    {
                        MembershipService.CreateUserGroup(model.GroupName, false, model.SiteCategory);

                        transaction.Commit();
                    }

                    result = RedirectToAction("Index");
                }
            }

            return result ?? View(model);
        }

        public virtual ActionResult Edit(int id)
        {
            ActionResult result = null;
            if (id > 0)
            {
                var data = UserGroupService.FindOne(id);
                if (data != null)
                {
                    result = PartialView(data);
                }
            }

            return result ?? PartialView(new CmsUserGroup());
        }

        [HttpPost]
        public virtual ActionResult Edit(CmsUserGroup model)
        {
            ActionResult result = null;
            ViewBag.UserGroupId = model.Id;

            if (string.IsNullOrEmpty(model.GroupName))
            {
                ModelState.AddModelError("GroupName", GetText("Please enter user group", "cms_Users"));
            }

            if (ModelState.IsValid)
            {
                using (var transaction = new BusinessServiceTransaction<CmsUserGroup>())
                {
                    MembershipService.UpdateUserGroup(model);

                    transaction.Commit();
                }

                result = RedirectToAction("Index");
            }

            return result ?? View(model);
        }

        public virtual ActionResult UserGroupDetails(CmsUserGroup model)
        {
            ActionResult result = null;
            if (model != null)
            {
                result = PartialView(model);
            }

            return result ?? PartialView(new CmsUserGroup());
        }

        public virtual ActionResult UserGroupProperties(int id)
        {
            return EntityProperty(id);
        }

        public virtual ActionResult UserGroupAnnotationSettings(int id)
        {
            CmsUserGroup data = null;

            if (id > 0)
            {
                data = UserGroupService.FindOne(id);
            }
            else
            {
                data = new CmsUserGroup();
            }

            string color = data.GetMetaValue<string>(CmsUserGroup.UserGroupColorMetaFieldName);
            if (string.IsNullOrWhiteSpace(color))
            {
                color = CmsUserGroup.DefaultUserGroupColor;
            }

            return PartialView(new { Color = color, UserGroupId = data.Id }.ToExpando());
        }

        [HttpPost]
        public virtual ActionResult UserGroupAnnotationSettings(int userGroupId, string color)
        {
            var data = UserGroupService.FindOne(userGroupId);

            if (data != null)
            {
                if (!string.IsNullOrWhiteSpace(color))
                {
                    data.SetMetaValue(CmsUserGroup.UserGroupColorMetaFieldName, color, true);
                }
            }

            return RedirectToAction("Index");
        }

        public override ActionResult EntityProperty(int id)
        {
            ViewBag.ControllerName = "CmsUserGroup";

            return base.EntityProperty(id);
        }

        #endregion View methods

        protected override bool _Delete(int id)
        {
            bool result = false;

            var cmsUserGroup = UserGroupService.FindOne(id);

            if (cmsUserGroup != null)
            {
                MembershipService.DeleteUserGroup(cmsUserGroup);

                result = true;
            }

            return result;
        }
    }
}